package com.logictech.lims.web.back;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.plugin.ContextHolderUtils;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.logictech.activity.system.vo.LoginUser;
import com.logictech.common.util.data.ExcelUtil;
import com.logictech.common.util.data.StringUtils;
import com.logictech.lims.dto.ReagentTypeDto;
import com.logictech.lims.dto.ReagentTypeListRowDto;
import com.logictech.lims.entity.Location;
import com.logictech.lims.entity.Position;
import com.logictech.lims.entity.ReagentType;
import com.logictech.lims.service.LocationService;
import com.logictech.lims.service.LogService;
import com.logictech.lims.service.PositionService;
import com.logictech.lims.service.ReagentTypeService;

/**
 * 描述：</b>ReagentTypeController<br>
 * 
 * @author guoqi
 * @since：2017年03月21日 16时10分47秒 星期二
 * @version:1.0
 */
@Controller
@RequestMapping("/lims/back/reagentType")
public class ReagentTypeController extends BaseController {
	@Autowired
	private ReagentTypeService reagentTypeService;

	@Autowired
	private PositionService positionService;

	@Autowired
	private LocationService locationService;
	
	@Autowired
	private LogService logService;

	/**
	 * 列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public void list(@ModelAttribute ReagentTypeDto query, HttpServletResponse response, HttpServletRequest request,
			@RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception {
		PageQuery<ReagentTypeDto> pageQuery = new PageQuery<ReagentTypeDto>();
		pageQuery.setPageNo(pageNo);
		pageQuery.setPageSize(pageSize);
		VelocityContext velocityContext = new VelocityContext();
		pageQuery.setQuery(query);
		velocityContext.put("reagentType", query);
		velocityContext.put("pageInfos", SystemTools.convertPaginatedList(reagentTypeService.queryPageList(pageQuery)));
		String viewName = "lims/back/reagentType-list.vm";
		request.getSession().setAttribute("pageQueryReagentTypeDto", pageQuery);
		ViewVelocity.view(request, response, viewName, velocityContext);
	}
	
	
	/**
	 * 返回列表页面
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "back", method = { RequestMethod.GET, RequestMethod.POST })
	public void back(@ModelAttribute ReagentTypeDto query, HttpServletResponse response, HttpServletRequest request,
			@RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception {
		PageQuery<ReagentTypeDto> pageQuery = new PageQuery<ReagentTypeDto>();
		
		Object session = request.getSession().getAttribute("pageQueryReagentTypeDto");
		if(session != null){
			pageQuery = (PageQuery<ReagentTypeDto>)session;
			query = pageQuery.getQuery();
		}else{
			pageQuery.setPageNo(pageNo);
			pageQuery.setPageSize(pageSize);
			pageQuery.setQuery(query);
		}
		VelocityContext velocityContext = new VelocityContext();
		velocityContext.put("reagentType", query);
		velocityContext.put("pageInfos", SystemTools.convertPaginatedList(reagentTypeService.queryPageList(pageQuery)));
		String viewName = "lims/back/reagentType-list.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}
	
	/**
	 * 导出列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "export", method = { RequestMethod.GET, RequestMethod.POST })
	public void export(@ModelAttribute ReagentTypeDto query, HttpServletResponse response, HttpServletRequest request) throws Exception {
		try{
			LoginUser user = (LoginUser) ContextHolderUtils.getSession().getAttribute("OPERATE_WEB_LOGIN_USER");
			logService.doAdd(user.getUserName() + "导出试剂类型信息");
			
			PageQuery<ReagentTypeDto> pageQuery = new PageQuery<ReagentTypeDto>();
			pageQuery.setQuery(query);
			List<ReagentTypeDto> list = reagentTypeService.queryList(pageQuery);
			LinkedHashMap<String, String> fieldMap = new LinkedHashMap<String, String>();
			fieldMap.put("englishName", "英文名称");
			fieldMap.put("chineseName", "中文名称");
			fieldMap.put("positionNo", "位置");
			fieldMap.put("locationNo", "库位");
			fieldMap.put("storageCondition", "存放条件");
			fieldMap.put("casNo", "CAS序列号");
			fieldMap.put("size", "一般规格(ml)");
			fieldMap.put("hazardPrecautions", "危害注意事项");
			fieldMap.put("protectiveCondition", "防护条件");
			fieldMap.put("alarmStorage", "警戒值");
			fieldMap.put("storage", "库存量");
			ExcelUtil.listToExcel(list, fieldMap, "试剂类型信息", response);
			
			
		}catch(Exception e){
			System.out.println(e.getStackTrace());
		}
	}

	/**
	 * 详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "toDetail", method = RequestMethod.GET)
	public void toDetail(@RequestParam(required = true, value = "id") String id, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "lims/back/reagentType-detail.vm";
		ReagentType reagentType = reagentTypeService.queryById(id);
		velocityContext.put("reagentType", reagentType);
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	/**
	 * 跳转到添加页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/toAdd", method = { RequestMethod.GET, RequestMethod.POST })
	public void toAdd(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "lims/back/reagentType-add.vm";

		// 位置列表
		List<Position> positionList = new ArrayList<Position>();
		positionList = this.positionService.queryByType("1");
		velocityContext.put("positionList", positionList);

		// 库位列表
		List<Location> locationList = new ArrayList<Location>();
		locationList = this.locationService.queryAll();
		velocityContext.put("locationList", locationList);

		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	/**
	 * 保存信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/doAdd", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public AjaxJson doAdd(@ModelAttribute ReagentType reagentType) {
		AjaxJson j = new AjaxJson();
		try {
			LoginUser user = (LoginUser) ContextHolderUtils.getSession().getAttribute("OPERATE_WEB_LOGIN_USER");
			reagentType.setCreateUser(user.getId().toString());
			reagentType.setCreateTime(new Date());
			reagentType.setUpdateUser(user.getId().toString());
			reagentType.setUpdateTime(new Date());
			reagentType.setDeleteFlag("0");
			reagentTypeService.doAdd(reagentType);
			j.setMsg("保存成功");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("保存失败");
		}
		return j;
	}

	/**
	 * 跳转到编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "toEdit", method = RequestMethod.GET)
	public void toEdit(@RequestParam(required = true, value = "id") String id, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		ReagentType reagentType = reagentTypeService.queryById(id);
		velocityContext.put("reagentType", reagentType);
		String viewName = "lims/back/reagentType-edit.vm";
		// 位置列表
		List<Position> positionList = new ArrayList<Position>();
		positionList = this.positionService.queryByType("1");
		velocityContext.put("positionList", positionList);

		// 库位列表
		List<Location> locationList = new ArrayList<Location>();
		locationList = this.locationService.queryAll();
		velocityContext.put("locationList", locationList);
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	/**
	 * 编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "/doEdit", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public AjaxJson doEdit(@ModelAttribute ReagentType reagentType) {
		AjaxJson j = new AjaxJson();
		try {
			LoginUser user = (LoginUser) ContextHolderUtils.getSession().getAttribute("OPERATE_WEB_LOGIN_USER");
			reagentType.setUpdateUser(user.getId().toString());
			reagentType.setUpdateTime(new Date());
			reagentTypeService.doEdit(reagentType);
			j.setMsg("编辑成功");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("编辑失败");
		}
		return j;
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "doDelete", method = RequestMethod.GET)
	@ResponseBody
	public AjaxJson doDelete(@RequestParam(required = true, value = "id") String id) {
		AjaxJson j = new AjaxJson();
		try {
			reagentTypeService.doDelete(id);
			j.setMsg("删除成功");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("删除失败");
		}
		return j;
	}
	
	/**
	 * 获取试剂列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = { "/getReagentTypeList" }, produces = "application/x-www-form-urlencoded;charset=UTF-8",method = { org.springframework.web.bind.annotation.RequestMethod.GET,
			org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
    public ReagentTypeListRowDto getReagentTypeList(@RequestParam(required = true, value = "name" )  String name){
		
		ReagentTypeListRowDto listRowDto = new ReagentTypeListRowDto();
        List<ReagentTypeDto> preparationTypeList = this.reagentTypeService.queryAll(name);
        listRowDto.setRows(preparationTypeList);
        listRowDto.setTotal(StringUtils.parseInt(preparationTypeList.size()));
        return listRowDto;
    }
	
}
